﻿using System;
using System.Configuration;
using System.IO;
using System.Net;
using System.Net.Http;
using System.Text;
using System.Windows.Forms;
using Newtonsoft.Json;

namespace StationChangeDemo
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }


        private void button1_Click_1(object sender, EventArgs e)
        {
            var host = textBox_url.Text.TrimEnd('/') + "/manage/event/s1?code=" + textBox_stationCode.Text +
                       "&WorkflowOrderId" + textBox_workorder.Text;

            var httpClient = new HttpClient();
            var postData = JsonConvert.SerializeObject(new { code = textBox_stationCode.Text });
            textBox_resp.AppendText(postData);
            var htpContent = new StringContent(postData, Encoding.UTF8, "application/json");

            var resp = httpClient.GetAsync(host).Result;
            if (resp.StatusCode == HttpStatusCode.OK)
                textBox_resp.AppendText("\r\n" + resp.Content.ReadAsStringAsync().Result);
            else
                MessageBox.Show(resp.StatusCode.ToString());
        }

        private void button2_Click(object sender, EventArgs e)
        {
            var host = textBox_url.Text.TrimEnd('/') + "/manage/event/s3" + "?code=" + textBox_stationCode.Text +
                       "&WorkflowOrderId=" + textBox_workorder.Text;
            ;

            var httpClient = new HttpClient();


            var resp = httpClient.GetAsync(host).Result;
            if (resp.StatusCode == HttpStatusCode.OK)
                textBox_resp.AppendText("\r\n" + resp.Content.ReadAsStringAsync().Result);
            else
                MessageBox.Show(resp.StatusCode.ToString());
        }

        private void button3_Click(object sender, EventArgs e)
        {
            var host = textBox_url.Text.TrimEnd('/') + "/manage/event/inspected" + "?code=" + textBox_stationCode.Text +
                       "&WorkflowOrderId=" + textBox_workorder.Text;
            ;

            var httpClient = new HttpClient();


            var resp = httpClient.GetAsync(host).Result;
            if (resp.StatusCode == HttpStatusCode.OK)
                textBox_resp.AppendText("\r\n" + resp.Content.ReadAsStringAsync().Result);
            else
                MessageBox.Show(resp.StatusCode.ToString());
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            textBox_url.Text = ConfigurationManager.AppSettings["host"];
        }

        private void button4_Click(object sender, EventArgs e)
        {
            var result = openFileDialog1.ShowDialog();
            if (result == DialogResult.OK)
            {
                var httpClient = new HttpClient();
                var str = "";
                using (var reader = new StreamReader(openFileDialog1.OpenFile()))
                {
                    var line = reader.ReadLine();
                    while (line != null)
                    {

                        var ary = line.Split('\t');
                        var code = ary[0];
                        var station = ary[1];
                        var host = textBox_url.Text.TrimEnd('/') + "/manage/event/s1" + "?code=" + station +
                                   "&WorkflowOrderId=" + code;
                        var resp = httpClient.GetAsync(host).Result;
                        textBox_resp.AppendText("\r\n" + resp.Content.ReadAsStringAsync().Result);
                    }
                }

            }
        }
    }
}